class Solution(object):
    def uniquePathsWithObstacles(self, obstacle_grid):
        """
        :type obstacle_grid: List[List[int]]
        :rtype: int
        """
        if len(obstacle_grid) <= 0:
            return 0
        if len(obstacle_grid[0]) <= 0:
            return 0
        for row in obstacle_grid:
            if len(row) != len(obstacle_grid[0]):
                return 0

        total_way = [[0] * len(obstacle_grid[0]) for _ in range(len(obstacle_grid))]
        for i in range(0, len(obstacle_grid)):
            for j in range(0, len(obstacle_grid[0])):
                total_way[i][j] = 0
                if i == 0 and j == 0 and obstacle_grid[i][j] != 1:
                    total_way[i][j] = 1
                if obstacle_grid[i][j] == 1:
                    continue
                if i > 0:
                    total_way[i][j] = total_way[i][j] + total_way[i - 1][j]
                if j > 0:
                    total_way[i][j] = total_way[i][j] + total_way[i][j - 1]

        return total_way[-1][-1]
